<?php
class TutorlandAccount extends Controller {

	function TutorlandAccount()
	{
		parent::Controller();
	}
	
	function index()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('BankModel');
	    $this->load->model('TutorLandAccountModel');
	    $data['accountList'] = $this->TutorLandAccountModel->getAll();
	    $data['bankList'] = $this->BankModel->getAll();
		$this->load->view('header');
		$this->load->view('tutorland-account-form', $data);
		$this->load->view('footer');
	}
	
	function save()
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('TutorLandAccountModel');
	    $id = $this->input->post('txtDel');
	    $accountID = $this->input->post('accountID');
	    $bankID = $this->input->post('bankID');
	    $branch = $this->input->post('txtBranch');
	    $name = $this->input->post('txtName');
	    $type = $this->input->post('selType');
	    $no = $this->input->post('txtNo');
	    
	    for($i = 0; $i < count($bankID); $i++) {
	    	$data = array();
	    	$data['bank_id'] = $bankID[$i];
    		$data['bank_branch'] = $branch[$i];
    		$data['account_type'] = $type[$i];
    		$data['account_no'] = $no[$i];
    		$data['account_name'] = $name[$i];
    		if($branch[$i] != null && $no[$i] != null && $name != null) {
		    	if(isset($accountID) == false || $accountID[$i] == null) {
		    		// add
		    		$data['is_active'] = '1';
		    		$this->TutorLandAccountModel->add($data);
		    	} else {
		    		// update
		    		$data['account_id'] = $accountID[$i];
		    		$this->TutorLandAccountModel->update($data);
		    	}
    		}
	    }
	    $this->index();
	}
	
	function delete()
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('TutorLandAccountModel');
	    $id = $this->input->post('txtDel');
	    
	    for($i = 0; $i < count($id); $i++) {
	    	if($id[$i] != null)
	    		$this->TutorLandAccountModel->delete($id[$i]);
	    }
	    $this->index();
	}
}
?>